﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using System.Reflection;
using log4net;
using System.Text;
namespace AnyOffice.general.ipanel
{
    public partial class ajax_online : Page
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
     
        private string WriteMessage()
        {
            new ConfigManager();
            StringBuilder builder = new StringBuilder();
            int num = 0;
            SqlConnection conn = new SqlConnection(ConfigManager.ConnectionString);
            DataTable table = new UsersDao().SelectOnline(conn, base.User.Identity.Name);
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    if (num == 0)
                    {
                        builder.Append("<table border=0 cellspacing=1 width=100% bgcolor=black cellpadding=3 class=small>");
                        builder.Append("<tr class=TableHeader onclick=clickMenu('" + table.Rows[i]["DeptId"].ToString() + "') style=cursor:hand>");
                        builder.Append("<td align=center>" + table.Rows[i]["DeptName"].ToString() + "</td>");
                        builder.Append("</tr>");
                        builder.Append("</table>");
                        builder.Append("<table border=0 cellspacing=1 cellpadding=3 width=100% bgcolor=black id='" + table.Rows[i]["DeptId"].ToString() + "' class=small>");
                        builder.Append("<tr class=TableData align=center>");
                        builder.Append("<td>&nbsp;");
                        builder.Append("<img title=" + this.GetState(Convert.ToInt32(table.Rows[i]["State"].ToString())) + " src=/images/on_status" + table.Rows[i]["State"].ToString() + ".gif align=absmiddle>&nbsp;");
                        builder.Append("</td>");
                        builder.Append("<td align=center>" + table.Rows[i]["UserName"].ToString() + "</td>");
                        builder.Append("<td width=70><a href=javascript:parent.send_sms('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>消息</a>&nbsp;<a href=javascript:parent.send_email('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>邮件</a></td>");
                        builder.Append("</tr>");
                    }
                    else if (num != Convert.ToInt32(table.Rows[i]["DeptId"].ToString()))
                    {
                        builder.Append("</table>");
                        builder.Append("<table border=0 cellspacing=1 width=100% bgcolor=black cellpadding=3 class=small>");
                        builder.Append("<tr class=TableHeader onclick=clickMenu('" + table.Rows[i]["DeptId"].ToString() + "') style=cursor:hand>");
                        builder.Append("<td align=center>" + table.Rows[i]["DeptName"].ToString() + "</td>");
                        builder.Append("</tr>");
                        builder.Append("</table>");
                        builder.Append("<table border=0 cellspacing=1 cellpadding=3 width=100% bgcolor=black id='" + table.Rows[i]["DeptId"].ToString() + "' class=small>");
                        builder.Append("<tr class=TableData align=center>");
                        builder.Append("<td>&nbsp;");
                        builder.Append("<img title=" + this.GetState(Convert.ToInt32(table.Rows[i]["State"].ToString())) + " src=/images/on_status" + table.Rows[i]["State"].ToString() + ".gif align=absmiddle>&nbsp;");
                        builder.Append("</td>");
                        builder.Append("<td align=center>" + table.Rows[i]["UserName"].ToString() + "</td>");
                        builder.Append("<td width=70><a href=javascript:parent.send_sms('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>消息</a>&nbsp;<a href=javascript:parent.send_email('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>邮件</a></td>");
                        builder.Append("</tr>");
                    }
                    else
                    {
                        builder.Append("<tr class=TableData align=center>");
                        builder.Append("<td>&nbsp;");
                        builder.Append("<img title=" + this.GetState(Convert.ToInt32(table.Rows[i]["State"].ToString())) + " src=/images/on_status" + table.Rows[i]["State"].ToString() + ".gif align=absmiddle>&nbsp;");
                        builder.Append("</td>");
                        builder.Append("<td align=center>" + table.Rows[i]["UserName"].ToString() + "</td>");
                        builder.Append("<td width=70><a href=javascript:parent.send_sms('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>消息</a>&nbsp;<a href=javascript:parent.send_email('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>邮件</a></td>");
                        builder.Append("</tr>");
                    }
                    num = Convert.ToInt32(table.Rows[i]["DeptId"].ToString());
                }
                builder.Append("</table>");
            }
            return builder.ToString();
        }
        private string GetState(int state)
        {
            switch (state)
            {
                case 1:
                    return "联机";

                case 2:
                    return "忙碌";

                case 3:
                    return "离开";
            }
            return "联机";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.AppendHeader("Expires", DateTime.Now.AddDays(-1.0).ToString("r"));
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
            try
            {
                Response.Write(this.WriteMessage());
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
        }
    }
}
